/*
-------------------------------------------------------------------
BIE is Copyright 2001-2004 Brunswick Corp.
-------------------------------------------------------------------
Please read the legal notices (docs/legal.txt) and the license
(docs/bie_license.txt) that came with this distribution before using
this software.
-------------------------------------------------------------------

*/
package com.webdeninteractive.bie.persistent.privileges;

import java.sql.*;
import java.util.*;
import com.webdeninteractive.bie.persistent.*;
import java.io.*;

/** An individual person known to BIE. */
public final class User extends Persistent {
    
    private int oid;
    private String name;
    private String username;
    private String passphrase;
    private String emailAddress;
    private String md5Signature;
    private String ipPattern;
    private String timeRange;
    private java.util.Date expires;
    
    private User(int oid,String username) {
        this.oid = oid;
        this.username=username;
    }
    
    public int getOID() {
        return this.oid;
        
    }
    
    /** Return the username value. */
    public String getUserName() {
        return username;
    }
    
    /** Return the username value. */
    public String getName() {
        return name;
    }
    
    /** Modify the name value. */
    public void	setName(String name) {
        this.name =  name;
    }
    
    /** Modify the passphrase value. */
    public String getPassphrase() {
        return this.passphrase ;
    }
    
    /** Modify the passphrase value. */
    public void	setPassphrase(String passphrase) {
        this.passphrase =  passphrase;
    }
    
    /** Return the email address value. */
    public String getEmailAddress() {
        return emailAddress;
    }
    
    /** Modify the email address value. */
    public void	setEmailAddress(String emailAddress) {
        this.emailAddress =  emailAddress;
    }
    /** Return the MD5 signature value. */
    public String getMd5Signature() {
        return md5Signature;
    }
    
    /** Modify the MD5 signature value. */
    public void	setMd5Signature(String md5Signature) {
        this.md5Signature = md5Signature;
    }
    
    /** Return the IP pattern value. */
    public String	getIpPattern() {
        return ipPattern;
    }

    /** Modify the IP pattern value. */
    public void	setIpPattern(String ipPattern) {
        this.ipPattern =  ipPattern;
    }
    
    /** Return the time range value. */
    public String getTimeRange() {
        return timeRange;
    }
    
    /** Modify the time range value. */
    public void	setTimeRange(String timeRange) {
        this.timeRange =  timeRange;
    }
    
    /** Return the expiry value. */
    public java.util.Date getExpires() {
        return expires;
    }
    
    /** Modify the expiry value. */
    public void	setExpires(java.util.Date expiry) {
        this.expires =  expires;
    }
    
    /** Create a new User with the given (unique) username.*/
    public static User	create(String username, String passphrase) throws SQLException,IOException {
        Connection conn = null;
        Statement s = null;
        int incrementer = Controller.getInstance().generateOID("Users");
        
        try {
            
            conn = DataSourceFactory.getConnection();
            s = conn.createStatement();
            
            String sql = "insert into Users (OID,username,name,passphrase,md5Signature,ipPattern,timeRange,expires,emailAddress) ";
            sql += "values (" + incrementer + ",'" + username + "','','" + passphrase + "','','','',0,'')";
            
            s.execute(sql);
            conn.commit();
        }finally {
            try {
                if (s != null) s.close();
                if (conn != null) conn.close();
            }catch (Exception e2) {}
        }
        
        User user = new User(incrementer,username);
        user.setPassphrase(passphrase);
        return user;
    }
    
    /** Return the existing User with the given object ID. */
    public static User	findByOID(int OID) throws SQLException,IOException {
        Connection conn = null;
        Statement s = null;
        ResultSet rs = null;
        try {
            conn = DataSourceFactory.getConnection();
            s = conn.createStatement();
            rs = s.executeQuery("select * from Users where OID = " + OID);
            rs.next();
            return load(rs);
        }finally {
            try {
                if (rs != null) rs.close();
                if (s != null) s.close();
                if (conn != null) conn.close();
            }catch (Exception e2) {}
        }
    }
    
    /** Return all existing Users. */
    public static ArrayList findAll() throws SQLException,IOException {
        Connection conn = null;
        Statement s = null;
        ResultSet rs = null;
        try {
            conn = DataSourceFactory.getConnection();
            s = conn.createStatement();
            ArrayList al = null;
            al = new ArrayList();
            rs = s.executeQuery("select * from Users");
            
            while (rs.next()) {
                al.add(load(rs));
            }
            return al;
        }finally {
            try {
                if (rs != null) rs.close();
                if (s != null) s.close();
                if (conn != null) conn.close();
            }catch (Exception e2) {}
        }
    }
    
    /** Return the existing User with the given username. */
    public static User	findByName(String username) throws SQLException,IOException {
        Connection conn = null;
        Statement s = null;
        ResultSet rs = null;
        
        try {
            conn = DataSourceFactory.getConnection();
            s = conn.createStatement();
            
            rs = s.executeQuery("select * from Users where username = '" + username + "'");
            rs.next();
            return load(rs);
        }finally {
            try {
                if (rs != null) rs.close();
                if (s != null) s.close();
                if (conn != null) conn.close();
            }catch (Exception e2) {}
        }
    }
    
    public void commitObject() throws SQLException,IOException{
        Connection conn = null;
        Statement s = null;
        ResultSet rs = null;
        
        try {
            conn = DataSourceFactory.getConnection();
            s = conn.createStatement();
            String sql ="update Users set name='" + this.name + "',passphrase='" + this.passphrase+ "',md5Signature='" + this.md5Signature+ "',ipPattern='" + this.ipPattern+ "',timeRange='" + this.timeRange+ "',expires=" + this.expires.getTime()+ ",emailAddress='" + this.emailAddress + "' where OID=" + this.oid;
            s.execute(sql);
            conn.commit();
            
        }finally {
            try {
                if (rs != null) rs.close();
                if (s != null) s.close();
                if (conn != null) conn.close();
            }catch (Exception e2) {}
        }
        
    }
    
    public void remove() throws SQLException,IOException{
        Connection conn = null;
        Statement s = null;
        
        if (this.oid == 1) {
            throw new SQLException("The admin user can not be removed");
        }
        
        try {
            conn = DataSourceFactory.getConnection();
            s = conn.createStatement();
            String sql ="delete from Users where OID=" + this.oid;;
            s.execute(sql);

            sql ="delete from User_Role where userOID=" + this.oid;;
            s.execute(sql);
                        
            conn.commit();            
        }finally {
            try {
                if (s != null) s.close();
                if (conn != null) conn.close();
            }catch (Exception e2) {}
        }        
    }
    
    
    private static User load(ResultSet rs) throws SQLException,IOException{
        User user = new User(rs.getInt("OID"),rs.getString("username"));
        user.setName(rs.getString("name"));
        user.setPassphrase(rs.getString("passphrase"));
        user.setEmailAddress(rs.getString("emailAddress"));
        user.setMd5Signature(rs.getString("md5Signature"));
        user.setIpPattern(rs.getString("ipPattern"));
        user.setTimeRange(rs.getString("timeRange"));
        user.setExpires(new java.util.Date(rs.getLong("expires")));
        return user;
    }
    
}

/*
                sql = "insert into Users (OID,username,name,passphrase,md5Signature,ipPattern,timeRange,expires,emailAddress) ";
                sql += "values (" + this.oid + ",'" + this.username + "','" + this.passphrase + "','" + this.md5Signature + "','" + this.ipPattern + "','" + this.timeRange + "'," + this.expires.getTime() + ",'" + this.emailAddress +"')";
 */